//
//  ZCHarmknowDetailVC.m
//  zctx
//
//  Created by apple on 16/3/9.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCHarmknowDetailVC.h"
#import "SXHTTPManager.h"
#import "ActionView.h"
@interface ZCHarmknowDetailVC ()<UIWebViewDelegate>

@property (nonatomic,strong)UIWebView *webView;
@end

@implementation ZCHarmknowDetailVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self loadHarmData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = [NSString stringWithFormat:@"危害详情"];;
    [self initnavegation];
    [self.view addSubview:self.webView];

    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self loadHarmData];
    });
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 24, 22);
    [rightButton addTarget:self action:@selector(shareButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_rightNavBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    [rightButton setImage:[UIImage imageNamed:@"Involved_tshare"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = barButtonItem_rightNavBar;
    
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 分享点击
-(void)shareButtonPressed:(id *)button{
    ActionView *action = [[ActionView alloc]initWithFrame:CGRectZero];
    action.pid = self.self.newsId;
    action.isweb =1;
    [action showInView:self.view];
}

-(void)loadHarmData{
    
    NSString *strurls = [NSString stringWithFormat:@"%@/Index/get_htmlencode_by_type?id=%@",[NetworkManager strURLHeader],self.newsId];
    
    NSLog(@"self.strURL = %@",strurls);
    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    [[SXHTTPManager manager] GET:strurls parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            
            NSDictionary *dci = [dict objectForKey:@"content"];
            NSString *str = [dci objectForKey:@"content"];
            
            NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]};
            
            NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NSData dataWithData:[str dataUsingEncoding:NSUTF8StringEncoding]] options:options documentAttributes:nil error:nil];
            
            NSString *resplacString = [NSString stringWithFormat:@"%@%@",@"src=\"",[NetworkManager strURLBasic]];
            //            NSLog(@"resplacString = %@",resplacString);
            NSString *strHtml = [attrStr.string stringByReplacingCharactersInRangeString:attrStr.string andString:@"src=\"" andorString:resplacString];
            
            NSMutableString *body = [NSMutableString string];
            [body appendFormat:@"<div class=\"title\">%@</div>",[dci objectForKey:@"title"]];
            [body appendFormat:@"<div class=\"ss\"><hr></div>"];
            
            [body appendString:strHtml];
            [self showInWebView:body];
        }
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        self.bannerView.hidden = NO;
    }];
}
#pragma mark - ******************** 拼接html语言
- (void)showInWebView:(NSString *)toBody
{
    NSMutableString *html = [NSMutableString string];
    [html appendString:@"<html>"];
    [html appendString:@"<head>"];
    [html appendFormat:@"<link rel=\"stylesheet\" href=\"%@\">",[[NSBundle mainBundle] URLForResource:@"ZCHarmDetails.css" withExtension:nil]];
    
    [html appendString:@"<style>"];
    [html appendString:@"img{width:100%;}"];
    [html appendString:@"table{width:100%;}"];
    [html appendString:@"</style>"];
    
    [html appendString:@"</head>"];
    
    [html appendString:@"<body style=\"background:#ffffff;padding:15;margin:0;\">"];
    [html appendString:toBody];
    [html appendString:@"</body>"];
    
    [html appendString:@"</html>"];
    
//    NSLog(@"html = %@",html);
    
    [self.webView loadHTMLString:html baseURL:nil];
}


#pragma mark webViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    self.bannerView.hidden = YES;

}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    self.bannerView.hidden = NO;
}

- (UIWebView *)webView
{
    if (!_webView)
    {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
    }
    return _webView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
